<html>
<head><meta charset="utf-8"><title>WorkerLocal type in rustc rayon fork · t-compiler/wg-parallel-rustc · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/index.html">t-compiler/wg-parallel-rustc</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html">WorkerLocal type in rustc rayon fork</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224118580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224118580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ammar Askar <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224118580">(Jan 26 2021 at 23:14)</a>:</h4>
<p>Hey there, while scanning crates for some soundness problems, we came across the fork of rayon (<a href="https://github.com/rust-lang/rustc-rayon">https://github.com/rust-lang/rustc-rayon</a>) that rustc uses in parallel mode. The <code>WorkerLocal</code> type it has seems to implement <code>Send</code> and <code>Sync</code> unconditionally for all types <code>T</code>: <a href="https://github.com/rust-lang/rustc-rayon/blob/ae7bbbd2756a324c493aef5f5d52473101b2f491/rayon-core/src/worker_local.rs#L10-L19">https://github.com/rust-lang/rustc-rayon/blob/ae7bbbd2756a324c493aef5f5d52473101b2f491/rayon-core/src/worker_local.rs#L10-L19</a></p>
<p>While the current inhabitants of <code>T</code> in rustc seem sound, i.e they're fine to be sent across threads, should this be bound by <code>T: Send</code> to avoid someone accidentally using a non-Sendable type like <code>Rc</code> across threads? Would this be worth making an issue about on the rust repo?</p>
<p>(cc <span class="user-mention" data-user-id="116466">@Zoxc</span> who wrote the <code>WorkerLocal</code> type; <span class="user-mention" data-user-id="329529">@Yechan Bae</span> part of the team doing the scanning)</p>



<a name="224120628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224120628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224120628">(Jan 26 2021 at 23:41)</a>:</h4>
<p>yes, <code>T: Send</code> seems appropriate</p>



<a name="224120710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224120710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224120710">(Jan 26 2021 at 23:42)</a>:</h4>
<p><del>and matching <code>T: Sync</code> for <code>Sync</code></del> (edit: can still be <code>T: Send</code> as long as it's not enabling sharing)</p>



<a name="224120769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224120769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224120769">(Jan 26 2021 at 23:43)</a>:</h4>
<p>I think <code>Deref</code> to <code>&amp;T</code> is most often how it would be used</p>



<a name="224120903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224120903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224120903">(Jan 26 2021 at 23:45)</a>:</h4>
<p>actually, I wonder why auto-impl isn't good enough</p>



<a name="224124032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224124032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224124032">(Jan 27 2021 at 00:21)</a>:</h4>
<p>Maybe I misunderstand the design as documented in the comments there; Isn't there an attempt in <code>WorkerLocal</code> to dynamically enforce a rule that you cannot access the underlying <code>T</code> from other threads?</p>



<a name="224124282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224124282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224124282">(Jan 27 2021 at 00:25)</a>:</h4>
<p><code>WorkerLocal::into_inner</code> collects them all into <code>Vec&lt;T&gt;</code></p>



<a name="224124325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224124325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224124325">(Jan 27 2021 at 00:25)</a>:</h4>
<p>and they're initially created locally in a simple range-map-collect</p>



<a name="224124465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224124465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224124465">(Jan 27 2021 at 00:27)</a>:</h4>
<p>so ultimately it is shipping <code>T</code> across threads, though I guess it does enforce that <code>T</code> won't be <em>shared</em> across threads -- so <code>T: Send</code> is fine</p>



<a name="224124771"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224124771" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224124771">(Jan 27 2021 at 00:30)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Cell</span>::<span class="n">new</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">locals</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">WorkerLocal</span>::<span class="n">new</span><span class="p">(</span><span class="o">|</span><span class="n">_</span><span class="o">|</span><span class="w"> </span><span class="o">&amp;</span><span class="n">data</span><span class="p">);</span><span class="w"> </span><span class="c1">// T = &amp;Cell&lt;_&gt;</span>
<span class="c1">// now threadpool accesses are sharing the same cell!</span>
</code></pre></div>



<a name="224125453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224125453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224125453">(Jan 27 2021 at 00:38)</a>:</h4>
<p>now i see, okay</p>



<a name="224131491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224131491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ammar Askar <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224131491">(Jan 27 2021 at 02:04)</a>:</h4>
<p>Probably because the auto-impl would restrict it to the tighter <code>T: Sync</code> bound for <code>Sync</code> whereas it should just be <code>Send</code></p>



<a name="224133344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal%20type%20in%20rustc%20rayon%20fork/near/224133344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ammar Askar <a href="https://rust-lang.github.io/zulip_archive/stream/187679-t-compiler/wg-parallel-rustc/topic/WorkerLocal.20type.20in.20rustc.20rayon.20fork.html#224133344">(Jan 27 2021 at 02:36)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/81425">https://github.com/rust-lang/rust/issues/81425</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>